<?php

namespace App\Http\Controllers\Admin;

use App\Exports\SignUpExport;
use App\Http\Controllers\Controller;
use App\Models\Activitys;
use App\Models\SignUps;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class SignUpController extends Controller {

    /**
     * 页面
     * @return view
     */
    public function index(){
        //获取活动
        $activitys = Activitys::get(['id as value','title as label'])->toArray();
        $activitys = json_encode($activitys);
        return view('admin.activity.sign_up',compact('activitys'));
    }

    /**
     * 列表数据
     * @param Request $request
     * @return bool
     */
    public function list(Request $request){
        try{
            $params = [
                'kwd' => (string)$request->get('kwd',''),
                'status' => (int)$request->get('status',0),
                'charge' => (int)$request->get('charge',0),
                'pay_state' => (int)$request->get('pay_state',0),
                'activity_id' => (int)$request->get('activity_id',0),
                'start' => (string)$request->get('start',''),
                'end' => (string)$request->get('end',''),
                'sort' => (string)$request->get('sort',''),
            ];
            $lists = SignUps::getList($params);
            foreach ($lists as $k=>$v){
                $lists[$k]['money'] = floatval($v['money']);
                $lists[$k]['price'] = floatval($v['price']);
            }
            return $this->writeJson(0,$lists);
        }catch (\Exception $e){
            return $this->writeJson(1,null,$e->getMessage());
        }
    }

    /**
     *  Excel导出
     * @param Request $request
     * @return bool
     */
    public function exports(Request $request)
    {
        $ids = $request->get('ids');
        $export = new SignUpExport($ids);
        //这里可以传参到服务层
        return Excel::download($export, '报名记录表'.date("Y-m-d H:i:s").'.xlsx');
    }

}
